package org.lyq.erp.config.listener;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.lyq.erp.auth.resources.pojo.Resources;
import org.lyq.erp.auth.resources.service.ResourcesService;
import org.lyq.erp.config.constant.ConfigConstant;
import org.lyq.erp.utils.string.ObjectEmptyUtils;
import org.lyq.erp.utils.string.StringEncode;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * 全局项目监听器
 * 
 * @ClassName: ContextListener
 * @Description:
 * @author LQY(廖先生)
 * @date 2017年12月29日 下午12:49:48
 *
 */
public class ContextListener implements ServletContextListener {
	/**
	 * 初始化方法，判定数据库类型和查询所有资源，且存储到servletContext域对象中。
	 */
	public void contextInitialized(ServletContextEvent event) {

		ServletContext servletContext = event.getServletContext();

		// 查询所有的资源
		WebApplicationContext appctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		// 获取资源模块的业务接口类
		ResourcesService resourcesService = appctx.getBean(ResourcesService.class);

		// 获取所有的资源
		List<Resources> allRes = resourcesService.selectALlResources();
		StringBuilder sb = new StringBuilder();
		for (Resources res : allRes) {
			sb.append(res.getResUrl()).append(ConfigConstant.SPLIT_STR);
		}
		servletContext.setAttribute(ConfigConstant.ALL_RES, sb.toString());

		// 读取数据库配置文件
		InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(ConfigConstant.CONFIG_PROPERTIRS_FILE_NAME);
		Properties properties = new Properties();

		try {
			properties.load(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 系统中拦截器不拦截的请求
		String donotInterUrl = properties.getProperty(ConfigConstant.DONOT_INTERCEPTOR_URL);
		if (ObjectEmptyUtils.isNotEmptyString(donotInterUrl)) {
			String[] donotUrls = donotInterUrl.split(ConfigConstant.SPLIT_STR);
			servletContext.setAttribute(ConfigConstant.DONOT_INTERCEPTOR_URL, donotUrls);
		}

		// 系统中被迫下线中拦截的请求
		String loginoutUrl = properties.getProperty(ConfigConstant.IS_INTERCEPTOR_LOGINOUT_URL);
		if (ObjectEmptyUtils.isNotEmptyString(loginoutUrl)) {
			String[] urls = loginoutUrl.split(ConfigConstant.SPLIT_STR);
			servletContext.setAttribute(ConfigConstant.IS_INTERCEPTOR_LOGINOUT_URL, urls);
		}

		// 系统中的超级管理用户
		String admin_id = properties.getProperty(ConfigConstant.ADMIN_ID);
		if (ObjectEmptyUtils.isNotEmptyString(admin_id)) {
			String[] ids= admin_id.split(ConfigConstant.SPLIT_STR);
			servletContext.setAttribute(ConfigConstant.ADMIN_ID, ids);
		}

		servletContext.setAttribute(ConfigConstant.SHOP_NAME, StringEncode.changeEncodeUTF8(properties.getProperty(ConfigConstant.SHOP_NAME)));
	}

	public void contextDestroyed(ServletContextEvent servletContextEvent) {

	}
}